package com.example.myapplication.dataanalysis;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.NewslistsBean;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class DataanalysisActivity extends AppCompatActivity {
    private ImageView but_Categorypage;
    private BarChart barChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dataanalysis);
        initview();

        newslist();
        but_bloak();
    }

    private void newslist() {
        OKHttpTool.doNetRequest("prod-api/api/movie/press/press/list",
                OKHttpTool.GET,
                NewslistsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(DataanalysisActivity.this, new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                NewslistsBean newslistsBean = (NewslistsBean) obj;
                                if (newslistsBean.getCode() == 200) {
                                    setbarchart(newslistsBean.getRows());
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void setbarchart(List<NewslistsBean.RowsDTO> rows) {
        if(rows ==null){
            return;
        }

        //设置柱状图的数值是否显示在柱子上方
        barChart.setDrawValueAboveBar(true);


        // 设置描述文本
        ArrayList<BarEntry> entries = new ArrayList<>();
        String[] strings = new String[5];
        for (int i = 0; i < 5; i++) {
            entries.add(new BarEntry(i, rows.get(i).getLikeNum(),rows.get(i).getTitle()));
            strings[i] = rows.get(i).getTitle();
        }

       // 获取柱状图（barChart）的X轴（xAxis）
        XAxis xAxis = barChart.getXAxis();
        // 设置横坐标文本样式
        xAxis.setValueFormatter(new IndexAxisValueFormatter(strings));
        barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        barChart.getXAxis().setGranularity(1f);
        barChart.getXAxis().setGranularityEnabled(true);


        BarDataSet dataSet = new BarDataSet(entries, "新闻点赞数据分析");
        dataSet.setColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,Color.GRAY);

        BarData barData = new BarData(dataSet);
        barChart.setData(barData);

        //表示禁用右侧的坐标轴
        barChart.getAxisRight().setEnabled(false);
        //表示设置Y轴的动画效果，动画持续时间为1000毫秒。这样设置可以使得图表在显示的时候具有动态的效果，增加用户的交互性和可视性
        barChart.animateY(1000);


        //最后调用invalidate()方法刷新图表
        barChart.invalidate();
    }

    private void initview() {
        but_Categorypage = findViewById(R.id.but_Categorypage);
        barChart = findViewById(R.id.bar_chart);
    }

    private void but_bloak() {
        but_Categorypage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataanalysisActivity.this.finish();
            }
        });
    }
}